package com.wzh.product.service.impl;

import com.wzh.common.utils.R;
import com.wzh.product.dao.DiscountDao;
import com.wzh.product.entity.DiscountEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.product.dao.MydisDao;
import com.wzh.product.entity.MydisEntity;
import com.wzh.product.service.MydisService;


@Service("mydisService")
public class MydisServiceImpl extends ServiceImpl<MydisDao, MydisEntity> implements MydisService {

    @Autowired
    private DiscountDao discountDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String tel = (String) params.get("tel");
        IPage<MydisEntity> page = this.page(
                new Query<MydisEntity>().getPage(params),
                new QueryWrapper<MydisEntity>().eq("tel",tel)
        );

        return new PageUtils(page);
    }

    @Override
    public R add(MydisEntity mydis) {
        DiscountEntity discount = discountDao.selectById(mydis.getDisId());
        if (discount.getKeYong()<=0){
            return R.error("优惠券已领完");
        }
        baseMapper.insert(mydis);
        discount.setKeYong(discount.getKeYong()-1);
        discount.setYiLing(discount.getYiLing()+1);
        discountDao.updateById(discount);
        return R.ok("领取成功");
    }

}